#!/usr/bin/perl
#upgrade_setting.cgi

require './mdmjobs-lib.pl';
&ReadParse();
&foreign_require("upgrade","upgrade-lib.pl");	
&foreign_require("network","network-lib.pl");	
&header($text{'upgrade_detail'}, "", "index", 0, 1,undef,undef);

local @devlist1=&foreign_call("mdmsearch","list_controlledservs");
local @devlist=sort{$a->{'id'} cmp $b->{'id'}}@devlist1;
local $localversion=&foreign_call("upgrade", "get_current_version");
local $mode=mode1;
print "<table class=mode1 border=0 cellpadding=4 cellspacing=1 width=100%>\n";
print "<tr class=title1>\n";
print "<td><b>$text{'index_id'}</b></td>\n";
print "<td><b>$text{'index_hostname'}</b></td>\n";
print "<td><b>$text{'index_ip'}</b></td>\n";
print "<td><b>$text{'index_version'}</b></td> </tr>\n";

my $netinfo = &foreign_call("network", "get_interface","eth0");
my $hostname = &get_system_hostname();
print "<tr class=$mode><td>$text{'thisserv'}</td><td>$hostname</td><td>$netinfo->{'address'}</td><td>$localversion</td></tr>\n";		

&remote_error_setup(\&inst_error);

$p=0;
foreach $s (@devlist){
	local ($rh = "READ$p", $wh = "WRITE$p");
	pipe($rh, $wh);
	select($wh); $| = 1; select(STDOUT);
	
	local $hostid = $s->{'id'};
	$hostid =~ s/\[//;
	$hostid =~ s/\].*//;
	if (!fork()) {
		close($rh);
		local $rst;
		local @rst;
		&remote_foreign_require($hostid, "upgrade","upgrade-lib.pl");		
		$rst = &remote_foreign_call($hostid, "upgrade", "get_current_version");
		push(@rst,$rst);
		print $wh &serialise_variable(\@rst);
		close($wh);
		exit;
	}
	
	close ($wh);
	$p++;	
}	

local @data;
local $logoper = "Get the version of devices";
$p = 0;
foreach $s (@devlist) {
	if (/\[This server\]/){
		next;
	}
	local $rh = "READ$p";
	local $line = <$rh>;
	close($rh);
	
	local $rv = &unserialise_variable($line);
	@rv=@$rv;
	if ($mode eq 'mode1'){
		$mode='mode2';
	}else{
		$mode='mode1';
	}
	print "<tr class=$mode><td>$s->{'id'}</td><td>$s->{'hostname'}</td><td>$s->{'ip'}</td>\n";
	if ($rv[0]){
		push @data,"$s->{'id'}'s version is $rv[0]";
		print "<td>$rv[0]</td></tr>\n";
	}else{
		push @data,"Can't get $s->{'id'}'s version.";
		print "<td>$text{'noanswer'}</td></tr>\n";
	}
	
	$p++;	
}
&foreign_call("mdmhistory","mdm_log",$logoper,\@data);	

print "<form action=mdm_upgrade.cgi method=post >\n";
print "<table class=mode1 border=0 cellspacing=0 cellpadding=0>\n";
print "<br><tr><input type=submit class=button value=\"$text{'index_upgrade'}\"></tr>\n";
print "</table></form>\n";
&footer("index.cgi", $text{'index_title'});
1;
